function varargout = FCS_processor(varargin)
% FCS_PROCESSOR M-file for FCS_processor.fig
%      FCS_PROCESSOR, by itself, creates a new FCS_PROCESSOR or raises the existing
%      singleton*.
%
%      H = FCS_PROCESSOR returns the handle to a new FCS_PROCESSOR or the handle to
%      the existing singleton*.
%
%      FCS_PROCESSOR('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in FCS_PROCESSOR.M with the given input arguments.
%
%      FCS_PROCESSOR('Property','Value',...) creates a new FCS_PROCESSOR or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before FCS_processor_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to FCS_processor_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help FCS_processor

% Last Modified by GUIDE v2.5 03-Nov-2009 13:33:02

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @FCS_processor_OpeningFcn, ...
                   'gui_OutputFcn',  @FCS_processor_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before FCS_processor is made visible.
function FCS_processor_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to FCS_processor (see VARARGIN)

% Choose default command line output for FCS_processor
handles.output = hObject;
handles.hist_res=[1:10:2^18];
set(handles.popupmenu1,'String',{'2^15','2^12','2^10','2^8','2^6'});
set(handles.popupmenu1,'Value',4);
set(handles.popupmenu2,'String',{'2^18','2^15','2^12','2^10','2^8'});
set(handles.popupmenu2,'Value',5);
set(handles.popupmenu3,'String',{'moving','sgolay'});
set(handles.popupmenu3,'Value',1);

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes FCS_processor wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = FCS_processor_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[filename,pathname]=uigetfile('*.fcs');
if(filename==0)
else
    filepath=[pathname,filename];
    [handles.data,handles.params]=fca_readfcs(filepath);
    
    handles.params.gate(1)=create_gate(handles,handles.data,handles.params);

    %removes logrithmic binning information, however since we normally
    %aquire in linear mode, these values are usually zero and can be
    %removed.
    if(get(handles.checkbox3,'Value')==0)
        for i=1:length(handles.params.par)
            par(i)=rmfield(handles.params.par(i),{'decade','log','logzero'});
        end
        handles.params.par=par;
    end
    %This setups up the file table  
    fieldValues{1,1}=fieldnames(rmfield(handles.params,{'par', 'gate'}));
    fieldValues{1,2}=struct2cell(rmfield(handles.params,{'par', 'gate'}));
    set(handles.uitable2,'Data',compute_data_cell(fieldValues)');
    
    set(handles.listbox1,'String',{handles.params.par.name});
    set(handles.listbox2,'String',{handles.params.gate.type});
    
    updateDataTable(handles)
    guidata(hObject, handles);
end

% --- Executes on selection change in listbox1.
function listbox1_Callback(hObject, eventdata, handles)
% hObject    handle to listbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
updateDataTable(handles)


% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    logPlotFlag=get(handles.checkbox6,'Value');
    indicesToPlot=get(handles.listbox1,'Value');
    numbins=handles.params.par(indicesToPlot(1)).range;
    
    val = get(handles.popupmenu1,'Value');
    string_list = get(handles.popupmenu1,'String');
    numScaledBins = str2num(string_list{val});
    
    gateIndex=get(handles.listbox2,'Value');
    data1=handles.data(handles.params.gate(gateIndex).indices,indicesToPlot(1));
    
if(get(handles.uipanel1,'SelectedObject')==handles.radiobutton1)

    logAxisFlag=get(handles.checkbox2,'Value');
    modePlotFlag=get(handles.checkbox1,'Value');

    plot_histogram(logPlotFlag,logAxisFlag,modePlotFlag,indicesToPlot,...
        numbins,numScaledBins,data1,handles.params,handles.params.gate(gateIndex).par(indicesToPlot(1)).mode)
    
elseif(get(handles.uipanel1,'SelectedObject')==handles.radiobutton2)

    data2=handles.data(handles.params.gate(gateIndex).indices,indicesToPlot(2));     
    plot_scatter(logPlotFlag,indicesToPlot,numbins,numScaledBins,handles.params,data1,data2)
end


% --- Executes when selected object is changed in uipanel1.
function uipanel1_SelectionChangeFcn(hObject, eventdata, handles)
% hObject    handle to the selected object in uipanel1 
% eventdata  structure with the following fields (see UIBUTTONGROUP)
%	EventName: string 'SelectionChanged' (read only)
%	OldValue: handle of the previously selected object or empty if none was selected
%	NewValue: handle of the currently selected object
% handles    structure with handles and user data (see GUIDATA)
if(get(handles.uipanel1,'SelectedObject')==handles.radiobutton1)
    value=get(handles.listbox1,'Value');
    set(handles.listbox1,'Value',value(1));
    
    set(handles.listbox1,'Max',1);
    updateDataTable(handles)
    
    guidata(hObject, handles);
elseif(get(handles.uipanel1,'SelectedObject')==handles.radiobutton2)
    set(handles.listbox1,'Max',2);
    
    guidata(hObject, handles);
end

% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
parIndex=get(handles.listbox1,'Value');
gateIndex=get(handles.listbox2,'Value');

for i=1:length(parIndex)
    handles.params.gate(gateIndex).par(parIndex(i)).mode=...
        compute_mode_wrapper(handles,handles.data(handles.params.gate(gateIndex).indices,parIndex(i)),...
        handles.params.par(parIndex(i)).range,handles.params.par(parIndex(i)).name);
end
   
updateDataTable(handles)
guidata(hObject, handles);


function plot_histogram(logPlotFlag,logAxisFlag,modePlotFlag,indexToPlot,numbins,hist_res,data,params,mode)
hist_fig=figure;

%Determines if logrithmically distributed bins are required
if(logPlotFlag)
    scaled_bins=exp((1:hist_res)*(log(numbins)/hist_res));
    hist(data,scaled_bins); 
    [n x]=hist(data,scaled_bins);
    %set(get(gcf,'CurrentAxes'),'Marker','.');
else
    hist(data,hist_res);
    [n x]=hist(data,hist_res);
end
%Determines if a log axis is required
if(logAxisFlag)
    set(get(gcf,'CurrentAxes'),'XScale','Log');
end

xlabel([params.par(indexToPlot).name,' v:',params.par(indexToPlot).voltage]);
ylabel('counts')

%Determins if mode lines and mode values are printed on the plot
%if(get(handles.checkbox1,'Value')==1)
if(modePlotFlag)
    
    for i=1:length(mode)
        line([mode(i).value mode(i).value],[0 max(n)*1.05],'Color','r');
        text(mode(i).value,max(n)*1.10,num2str(fix(mode(i).value)));
        try
        line([mode(i).lower mode(i).lower],[0 max(n)*1.05],'Color','g');
        line([mode(i).upper mode(i).upper],[0 max(n)*1.05],'Color','g');
        catch
            %'no mode boundaries'
        end
    end
end

%function plot_scatter(handles)
function plot_scatter(logPlotFlag,indicesToPlot,numbins,numScaledBins,params,data1,data2)
plot_fig=figure;

binReduction=numbins/numScaledBins;
dens = zeros(numScaledBins,numScaledBins);

%This rebins the data, by increasing the count in an index of a matix.
%The bins are resized so the matix is not incredibly large

if(logPlotFlag)
    for i=1:length(data1)
        x = max(fix(log(data1(i))*(numScaledBins/log(numbins))),1);
        y = max(fix(log(data2(i))*(numScaledBins/log(numbins))),1);
        dens(y,x) = dens(y,x) + 1;
    end    
    fsc_axis = 0:1:(numScaledBins-1)*binReduction;
    fl1_axis = 0:1:(numScaledBins-1)*binReduction;
else
    for i=1:length(data1)
        x = max(fix(data1(i)/binReduction),1);
        y = max(fix(data2(i)/binReduction),1);
        dens(y,x) = dens(y,x) + 1;
    end
    fsc_axis = 0:1:(numScaledBins-1)*binReduction;
    fl1_axis = 0:1:(numScaledBins-1)*binReduction;
end

gca=imagesc(fsc_axis, fl1_axis, log(dens+1), [0 4]);
axis xy
xlabel([params.par(indicesToPlot(1)).name,' v:',params.par(indicesToPlot(1)).voltage]);
ylabel([params.par(indicesToPlot(2)).name,' v:',params.par(indicesToPlot(2)).voltage]);
title([params.experiment,' ', params.specimen,' ',params.tube,': '...
    params.par(indicesToPlot(1)).name,' vs ',params.par(indicesToPlot(2)).name, ' scatter']);

function gate=create_gate(handles,data,params)
if(get(handles.uipanel8,'SelectedObject')==handles.radiobutton10)    
    gate.type='FSC-H greater less than';
    gate.parName='FSC-H';
    gate.parIndex=find(strcmp({params.par.name},gate.parName)==1);
    %gate.mode=NaN;
    gate.lower_bound=str2num(get(handles.edit5,'String'));
    gate.upper_bound=str2num(get(handles.edit7,'String'));
    gate.indices=find(data(:,gate.parIndex) > gate.lower_bound & ...
        data(:,gate.parIndex) < gate.upper_bound);
    gate.counts=length(gate.indices);
   
elseif(get(handles.uipanel8,'SelectedObject')==handles.radiobutton11)
    gate.type='% of FSC-H mode';
    gate.parName='FSC-H';
    gate.parIndex=find(strcmp({params.par.name},gate.parName)==1);

    mode = compute_mode_wrapper(handles,data(:,gate.parIndex),...
        params.par(gate.parIndex).range,gate.parName);
    percent = str2double(get(handles.edit6,'String'));
    gate.lower_bound=mode(1).value*(1-percent/100);
    gate.upper_bound=mode(1).value*(1+percent/100);
    gate.indices=find(data(:,gate.parIndex) > gate.lower_bound & ...
        data(:,gate.parIndex) < gate.upper_bound);
    gate.counts=length(gate.indices);
    gate.mode = mode;
    gate.percent = percent;
   
elseif(get(handles.uipanel8,'SelectedObject')==handles.radiobutton12)
    gate.type='No gating';
    gate.parName=NaN;
    gate.parIndex=NaN;
    %gate.mode = NaN;
    gate.lower_bound=NaN;
    gate.upper_bound=NaN;
    gate.indices=1:params.TotalEvents;
    gate.counts=length(gate.indices);
    
end
for i=1:length(params.par)
    gate.par(i).mean=mean(data(gate.indices,i)); 
    gate.par(i).mode=compute_mode_wrapper(handles,data(gate.indices,i),params.par(i).range,params.par(i).name);
    %gate.par(i).mode.value=NaN;
end


function mode=compute_mode_wrapper(handles,data,numbins,parName)

val = get(handles.popupmenu2,'Value');
string_list = get(handles.popupmenu2,'String');
binsize = str2num(string_list{val});

val2 = get(handles.popupmenu3,'Value');
string_list2 = get(handles.popupmenu3,'String');
smoother = string_list2{val2};
    
sensitivity=str2num(get(handles.edit3,'String'));
smooth_value=str2num(get(handles.edit4,'String'));
plotFlagDiff=get(handles.checkbox8,'Value');

%Determines if only -H channels or all channels should have mode data
%calculated
if(get(handles.uipanel6,'SelectedObject')==handles.radiobutton9)
    if(regexp(parName,'-H'))        
        if(get(handles.uipanel10,'SelectedObject')==handles.radiobutton13)          
            mode = compute_mode_diff(data,binsize,numbins,sensitivity,smoother,smooth_value,plotFlagDiff);
        elseif(get(handles.uipanel10,'SelectedObject')==handles.radiobutton14)         
            mode=compute_mode_fpeak(data,binsize,sensitivity,smoother,smooth_value);
            if(length(mode)>1 && length(mode) < 3)
                [mode ind centroid] = compute_mode_boundaries(data,mode);
            end
        end
    else
        mode.value=NaN;
    end
    
else   
    if(get(handles.uipanel10,'SelectedObject')==handles.radiobutton13)        
        mode = compute_mode_diff(data,binsize,numbins,sensitivity,smoother,smooth_value,plotFlagDiff);
    elseif(get(handles.uipanel10,'SelectedObject')==handles.radiobutton14)
        
        mode=compute_mode_fpeak(data,binsize,sensitivity,smoother,smooth_value);
        if(length(mode)>1 && length(mode) < 3)
            [mode ind centroid] = compute_mode_boundaries(data,mode);
        end
    end
end

% --- Executes when selected object is changed in uipanel10.
function uipanel10_SelectionChangeFcn(hObject, eventdata, handles)
% hObject    handle to the selected object in uipanel10 
% eventdata  structure with the following fields (see UIBUTTONGROUP)
%	EventName: string 'SelectionChanged' (read only)
%	OldValue: handle of the previously selected object or empty if none was selected
%	NewValue: handle of the currently selected object
% handles    structure with handles and user data (see GUIDATA)

if(get(handles.checkbox7,'Value'))
    if(get(handles.uipanel10,'SelectedObject')==handles.radiobutton13)
        set(handles.popupmenu2,'Value',5);
        set(handles.popupmenu3,'Value',1);
        set(handles.edit3,'String','0.5');
        set(handles.edit4,'String','11');
    elseif(get(handles.uipanel10,'SelectedObject')==handles.radiobutton14)
        set(handles.popupmenu2,'Value',3);
        set(handles.popupmenu3,'Value',2);
        set(handles.edit3,'String','150');
        set(handles.edit4,'String','11');
    end
end


function mode = compute_mode_fpeak(data,binsize,sensitivity,smoother,smooth_value)

%make a histogram of data
[x n]=hist(data, binsize);
r=smooth(x,smooth_value,smoother);
%fpeak function to find peaks in smoothed histogram data.  fpeak function
%has a tendancy to return peaks at the beginning and end of data, range
%variable at end prevents this
%peaks =fpeak(n,r,mode.sensitivity,[2 max(n)-1 10 Inf]);
%mode.values = peaks(:,1)';
peaks =fpeak(n,r,sensitivity,[2 max(n)-1 10 Inf]);
if(length(peaks)<1)
    mode(1).value=NaN;
else
    
    for i=1:length(peaks(:,1))
        mode(i).type='fpeak';
        mode(i).binsize=binsize;
        mode(i).sensitivity=sensitivity;
        mode(i).smoother=smoother;
        mode(i).smooth_value=smooth_value;
        mode(i).value=peaks(i,1);
    end
end
function [mode ind centroid] = compute_mode_boundaries(data,mode)
numOfmodes=length(mode);
[ind centroid]=kmeans(log(data),numOfmodes,'dist','sqeuclidean');
for i=1:length(mode)
    mode(i).lower=min(data(ind==i));
    mode(i).upper=max(data(ind==i));
    mode(i).counts=length(data(ind==i));
    mode(i).percentage=mode(i).counts/length(data);
end

function modeStruct = compute_mode_diff(data,binsize,numbins,sensitivity,smoother,smooth_value,plotflag)

%plotflag=0;
%generate logrithmic bins
scaled_bins=exp((1:binsize)*(log(numbins)/binsize));

[x nX]=hist(data,scaled_bins);
y=smooth(x,smooth_value,smoother);
z=diff(y);
zS=smooth(z,smooth_value,smoother);
zS(end+1)=0;

%nX
if(plotflag)
    figure
    bar(nX,zS);
    hold on
    set(get(gcf,'CurrentAxes'),'XScale','Log');
end
%mode.value=NaN;
%title(parName);

positive_value=1;
sign_prev=sign(positive_value);

cluster=1;
ind=zeros(length(zS),2);
startValue=10^1;
for i=startValue:length(zS)
    ind(i,1)=zS(i);
    
    if(abs(zS(i))>sensitivity)
        if(sign_prev==sign(zS(i)))
            ind(i,2)=cluster;
        else
            sign_prev=sign(zS(i));
            cluster=cluster+1;
            ind(i,2)=cluster;
        end
    else
        ind(i,2)=0;
    end
    
end
%ind
for j=1:cluster
    lower=min(nX(find(ind(:,2)==j)));
    upper=max(nX(find(ind(:,2)==j)));
    if(plotflag)
        line([lower lower],[0 max(zS)*1.05],'Color','g');
        line([upper upper],[0 max(zS)*1.05],'Color','r');
    end
end

for k=1:cluster/2
    
    modeStruct(k).type='diff';
    modeStruct(k).binsize=binsize;
    modeStruct(k).sensitivity=sensitivity;
    modeStruct(k).smoother=smoother;
    modeStruct(k).smooth_value=smooth_value;
    
    
    mLower=min(nX(find(ind(:,2)==(2*k-1))));
    mUpper=max(nX(find(ind(:,2)==(2*k))));
    mode_subset=data(data >mLower & data < mUpper);
    %length(mode_subset)
    %binsize=2^8;
    %Reduce the bin size one half the original order of magnitude
    binsize=fix(exp(log(length(mode_subset))/2));
    [v nV]=hist(mode_subset,binsize);
    
    %w=smooth(v,smooth_value,smoother);
    %log10(length(mode_subset))
    w=smooth(v,5,smoother);
    if(plotflag)
        figure
        bar(nV,w)
        title(['mode' num2str(k)]);
    end
    %length(w)
    % max(w)
    modeStruct(k).value=nV(find(w==max(w)));
    %modeStruct(k).value=NaN;
    modeStruct(k).lower=mLower;
    modeStruct(k).upper=mUpper;
    modeStruct(k).counts=length(mode_subset);
    modeStruct(k).percentage=modeStruct(k).counts/length(data);
    modeStruct(k).std=std(mode_subset);
end
%modeStruct

%modeStruct.value=NaN;



function dataCell = compute_data_cell(cell_array)
for i=1:length(cell_array)
    for j=1:length(cell_array{i})
        dataCell{i,j}=cell_array{i}{j};
    end
end
% --- Executes when selected object is changed in uipanel11.
function uipanel11_SelectionChangeFcn(hObject, eventdata, handles)
% hObject    handle to the selected object in uipanel11 
% eventdata  structure with the following fields (see UIBUTTONGROUP)
%	EventName: string 'SelectionChanged' (read only)
%	OldValue: handle of the previously selected object or empty if none was selected
%	NewValue: handle of the currently selected object
% handles    structure with handles and user data (see GUIDATA)
updateDataTable(handles)

function updateDataTable(handles)
if(get(handles.uipanel11,'SelectedObject')==handles.radiobutton15) 
    updateGateDataTable(handles,handles.params,handles.uitable1);
elseif(get(handles.uipanel11,'SelectedObject')==handles.radiobutton16) 
    updateParDataTable(handles,handles.uitable1);
elseif(get(handles.uipanel11,'SelectedObject')==handles.radiobutton17)
    updateModeDataTable(handles,handles.params,handles.uitable1);
end
    
function updateParDataTable(handles,table)
channelIndices=get(handles.listbox1,'Value');
gateIndex=get(handles.listbox2,'Value');

fieldValues{1,1}=fieldnames(handles.params.par(channelIndices(1)));
fieldValues{2,1}=struct2cell(handles.params.par(channelIndices(1)));

fieldValues{1,1}{end+1}='gate type';
fieldValues{2,1}{end+1}=handles.params.gate(gateIndex).type;
fieldValues{1,1}{end+1}='mean';
fieldValues{2,1}{end+1}=handles.params.gate(gateIndex).par(channelIndices(1)).mean;
constantFieldLength=length(fieldValues{1,1});

channelStruct=handles.params.gate(gateIndex).par(channelIndices(1));
%channelStruct

if(isnan(channelStruct.mode(1).value))  
else
    %'mode found'
    for i=1:length(channelStruct.mode)
        fieldValues{1,1}{constantFieldLength+i}=['mode ' num2str(i)];
        fieldValues{2,1}{constantFieldLength+i}=channelStruct.mode(i).value;
    end
end

if(get(handles.uipanel1,'SelectedObject')==handles.radiobutton1)    
elseif(get(handles.uipanel1,'SelectedObject')==handles.radiobutton2)
    if(length(channelIndices)>1)
        
        channelStruct=handles.params.gate(gateIndex).par(channelIndices(2));
        fieldValues{3,1}=struct2cell(handles.params.par(channelIndices(2)));
        fieldValues{3,1}{end+1}=handles.params.gate(gateIndex).type;
        fieldValues{3,1}{end+1}=handles.params.gate(gateIndex).par(channelIndices(2)).mean;

        if(isnan(channelStruct.mode(1).value))
        else
            %'mode found'
            for i=1:length(channelStruct.mode)
                fieldValues{1,1}{constantFieldLength+i}=['mode ' num2str(i)];
                fieldValues{3,1}{constantFieldLength+i}=channelStruct.mode(i).value;
            end
        end
    end
end
set(table,'Data',compute_data_cell(fieldValues)');

function updateGateDataTable(handles,params,table)

if(find(strcmp('mode',fieldnames(params.gate))==1))
    params.gate.mode=params.gate.mode(1).value;
end
fieldValues{1,1}=fieldnames(rmfield(params.gate,{'par','indices'}));
fieldValues{2,1}=struct2cell(rmfield(params.gate,{'par','indices'}));
%compute_data_cell(fieldValues)'
set(table,'Data',compute_data_cell(fieldValues)');

function updateModeDataTable(handles,params,table)
channelIndices=get(handles.listbox1,'Value');
gateIndex=get(handles.listbox2,'Value');

fieldValues{1,1}{1}='name';
fieldValues{2,1}{1}=handles.params.par(channelIndices(1)).name;
fieldValues{1,1}{2}='gate type';
fieldValues{2,1}{2}=handles.params.gate(gateIndex).type;
constantFieldLength=length(fieldValues{1,1});
channelStruct=handles.params.gate(gateIndex).par(channelIndices(1));

if(isnan(channelStruct.mode(1).value))
else

    fieldValues{1,1}{constantFieldLength+1}= 'mode';
    modeFieldNames=fieldnames(channelStruct.mode);
    for i=1:length(modeFieldNames)
        fieldValues{1,1}{end+1}=modeFieldNames{i};
    end
    
    for i=1:length(channelStruct.mode)
       fieldValues{i+1,1}{constantFieldLength+1}=['mode ' num2str(i)];
       modeFieldValues=struct2cell(channelStruct.mode(i));
       for j=1:length(modeFieldValues)
           fieldValues{i+1,1}{end+1}=modeFieldValues{j};
       end

    end
end
%fieldValues{1,1};
set(table,'Data',compute_data_cell(fieldValues)');

% --- Executes on button press in checkbox1.
function checkbox1_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox1


% --- Executes on button press in checkbox2.
function checkbox2_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox2


% --- Executes on button press in checkbox3.
function checkbox3_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox3



function edit3_Callback(hObject, eventdata, handles)
% hObject    handle to edit3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit3 as text
%        str2double(get(hObject,'String')) returns contents of edit3 as a double


% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit4_Callback(hObject, eventdata, handles)
% hObject    handle to edit4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit4 as text
%        str2double(get(hObject,'String')) returns contents of edit4 as a double


% --- Executes during object creation, after setting all properties.
function edit4_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end




% --- Executes during object creation, after setting all properties.
function uipanel1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to uipanel1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
% --- Executes during object creation, after setting all properties.
function radiobutton1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to radiobutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% --- Executes during object creation, after setting all properties.
function radiobutton2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to radiobutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% --- Executes during object creation, after setting all properties.
function pushbutton2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

function edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
% --- Executes during object creation, after setting all properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu1


% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in checkbox6.
function checkbox6_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox6



function edit6_Callback(hObject, eventdata, handles)
% hObject    handle to edit6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit6 as text
%        str2double(get(hObject,'String')) returns contents of edit6 as a double


% --- Executes during object creation, after setting all properties.
function edit6_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit5_Callback(hObject, eventdata, handles)
% hObject    handle to edit5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit5 as text
%        str2double(get(hObject,'String')) returns contents of edit5 as a double


% --- Executes during object creation, after setting all properties.
function edit5_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit7_Callback(hObject, eventdata, handles)
% hObject    handle to edit7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit7 as text
%        str2double(get(hObject,'String')) returns contents of edit7 as a double


% --- Executes during object creation, after setting all properties.
function edit7_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in listbox2.
function listbox2_Callback(hObject, eventdata, handles)
% hObject    handle to listbox2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns listbox2 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listbox2


% --- Executes during object creation, after setting all properties.
function listbox2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listbox2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in popupmenu2.
function popupmenu2_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu2 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu2


% --- Executes during object creation, after setting all properties.
function popupmenu2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in checkbox7.
function checkbox7_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox7


% --- Executes on selection change in popupmenu3.
function popupmenu3_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu3 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu3


% --- Executes during object creation, after setting all properties.
function popupmenu3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in checkbox8.
function checkbox8_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox8
function radiobutton1_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%set(handles.listbox1,'Max',1);
% Hint: get(hObject,'Value') returns toggle state of radiobutton1
% value=get(handles.listbox1,'Value');
% set(handles.listbox1,'Value',value(1));
% 
% set(handles.listbox1,'Max',1);
% updateDataTable(handles)
% 
% guidata(hObject, handles);

% --- Executes on button press in radiobutton2.
function radiobutton2_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%set(handles.listbox1,'Max',2);
% Hint: get(hObject,'Value') returns toggle state of radiobutton2
% set(handles.listbox1,'Max',2);
% 
% guidata(hObject, handles);
